/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cult.ticketing.dao;

import cult.ticketing.bean.Member;
import cult.ticketing.jdbc.DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author 51158282
 */
public class MemberDAO {
    /**
     * Get a member by email
     * @param email - a String specifying a member's email
     * @return a Member object, null if no email matches
     */
    public Member getMemberByEmail(String email) {
        Member member = null;

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM [member] where [email] = ?;");
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            if (rs != null && rs.next() != false) {
                member = new Member();
                member.setId(rs.getInt(1));
                member.setEmail(rs.getString(2));
                member.setPwd(rs.getString(3));
                member.setLoyaltyPoint(rs.getInt(4));
                member.setLoginStatus(rs.getBoolean(5));
                member.setRole(rs.getString(6));
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return member;
    }

    public int getLoyaltyPointById(int memberId) {
        int loyaltyPoint = 0;

        try {
            DataSource ds = DB.getDataSource();
            Connection con = ds.getConnection();
            PreparedStatement pstmt = con.prepareStatement("SELECT [loyalty_point] FROM [member] where [id] = ?;");
            pstmt.setInt(1, memberId);
            ResultSet rs = pstmt.executeQuery();
            if (rs != null && rs.next() != false) {
                loyaltyPoint = rs.getInt(1);
            }

            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return loyaltyPoint;
    }

    /**
     *
     * @param memberId - an Integer specifying a member's id
     * @param loginStatus - a Boolean specifying the login status to set (true: set to logged in; false: set to logged out)
     * @return a Boolean indicating if the set operation is successful
     */
    public boolean updateLoginStatusById(int memberId, boolean loginStatus) {
        boolean ifSuccessful = false;

        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("UPDATE [member] SET [login_status] = ? WHERE [id] = ?;");
            pstmt.setBoolean(1, loginStatus);
            pstmt.setInt(2, memberId);
            
            if (pstmt.executeUpdate() > 0) {
                ifSuccessful = true;
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return ifSuccessful;
    }

	
	public boolean addNewMember(String email, String pwd, String role) {
		boolean ifAddSuccessful = true;

                try {
                    DataSource ds = DB.getDataSource();
                    Connection con = ds.getConnection();
                    PreparedStatement pstmt = con.prepareStatement("INSERT INTO member([email], [pwd], [loyalty_point], [login_status], [role])VALUES(?, ?, 0, 0, 'customer');");
                        pstmt.setString(1, email);
                        pstmt.setString(2, pwd);

                        
                         int r = pstmt.executeUpdate();
                         if (r <= 0) {
                            ifAddSuccessful = false;
                        }
                        

                        if (pstmt != null) {
                            pstmt.close();
                        }
                        if (con != null) {
                            con.close();
                        }
                    } catch (NamingException ne) {
                       
                    } catch (SQLException sqle) {
                        
                    }

	
		return ifAddSuccessful;
	}
	
	public boolean deleteMemberById(int id) {
		boolean ifDeleteSuccessful = false;
		// TODO rs = delete .... from member where id = id
		
		return ifDeleteSuccessful;
	}
	
	public boolean setMemberById(int memberId, String email, String pwd) {
		boolean ifSetSuccessful = false;
		// TODO rs = update .... from member where id = memberId
		
		return ifSetSuccessful;
	}


}
